package org.java.xianxin.http;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.java.xianxin.util.StringUtils;

import java.io.File;
import java.util.Map;

/**
 * <p>
 * Http模拟请求增强类
 * </p>
 *
 * @author 敖燕飞
 * @since 上午11:21 2021/8/27
 */
@Slf4j
public class HttpUtilPlus extends HttpUtil {

    public static HttpResponse createPostUploadFile(String url, Map<String, String> headers, String name, File file) {
        return createHttpRequest(url, headers, null, "POST", null, name, file);
    }

    public static HttpResponse createGet(String url, Map<String, String> headers) {
        return createHttpRequest(url, headers, null, "GET", null, null);
    }

    public static HttpResponse createPost(String url, Map<String, String> headers) {
        return createPost(url, headers, null);
    }

    public static HttpResponse createPostByJson(String url, Map<String, String> headers, String body) {
        return createHttpRequest(url, headers, null, "POST", body, null);
    }

    public static HttpResponse createPost(String url, Map<String, String> headers, Map<String, Object> formMap) {
        return createHttpRequest(url, headers, formMap, "POST", null, null);
    }

    public static HttpResponse createHttpRequest(String url, Map<String, String> headers,
                                                 Map<String, Object> formMap, String method, String body,
                                                 String name, File... files) {
        log.info("内部转发请求Url：{}", url);
        HttpRequest httpRequest = null;

        if ("GET".equalsIgnoreCase(method)) {
            httpRequest = HttpUtil.createGet(url);
        } else if ("POST".equalsIgnoreCase(method)) {
            httpRequest = HttpUtil.createPost(url);
        }
        log.info("内部转发请求Method：{}", method);

        addHeader(httpRequest, headers);

        addFormMap(httpRequest, formMap);

        addBody(httpRequest, body);

        if (name != null) {
            addFiles(httpRequest, name, files);
        }

        HttpResponse httpResponse = httpRequest.execute();

        log.info("内部转发响应Status：{}", httpResponse.getStatus());
        log.info("内部转发响应Cookie：{}", httpResponse.getCookieStr());
        log.info("内部转发响应结果：{}", httpResponse.body());
        return httpResponse;
    }

    private static void addBody(HttpRequest httpRequest, String body) {
        if (!StringUtils.isEmpty(body)) {
            httpRequest.body(body);
            httpRequest.contentType("application/json;charset=UTF-8");
            log.info("内部转发请求参数：{}", body);
        }
    }

    private static void addFiles(HttpRequest httpRequest, String name, File[] files) {
        if (files != null && files.length > 0) {
            httpRequest.form(name, files);
            log.info("内部转发请求文件上传：{}={}", name, files);
        }
    }

    private static void addFormMap(HttpRequest httpRequest, Map<String, Object> formMap) {
        if (formMap != null && formMap.size() > 0) {
            httpRequest.form(formMap);
            log.info("内部转发请求参数：{}", JSON.toJSONString(formMap));
        }
    }

    private static void addHeader(HttpRequest httpRequest, Map<String, String> headers) {
        if (headers != null && headers.size() > 0) {
            for (String key : headers.keySet()) {
                String value = headers.get(key);
                httpRequest.header(key, value);
                log.info("内部转发请求Header：name={},value={}", key, value);
            }
        }
    }
}
